package File;

import File.domain.Student;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class file1 {
    static String path = "java语法\\src\\File\\Files";

    public static void main(String[] args) throws IOException, ClassNotFoundException {
//        createFile();
//        printFiles();
//        deleteFile("老婆.avi");
//        fileOutputStream();
//        fileInputStream();
//        fileCopy();
//        fileReader();
//        fileWriter();
//        bufferedStream();
//        buffered();
//        changer();
//        objectStream();
//        listStudent();
//        printStream();
//        zipInputStream();
        zipOutputStream();
    }

    public static void createFile() throws IOException {
        File file = new File(path);
        File newFile = new File(file, "老婆.avi");
        newFile.createNewFile();
    }

    public static void printFiles() {

        File file = new File(path);
        File[] files = file.listFiles();
        for (File file1 : files) {
//            if(file1.isFile()&&file1.getName().startsWith("老婆")){
//                System.out.println(file1);
//            }
            System.out.println(file1);
        }
    }

    public static void deleteFile(String child) {
        File file = new File(path);
        File dFile = new File(path, child);
        dFile.delete();
    }

    public static void fileOutputStream() throws IOException {
        FileOutputStream f1 = new FileOutputStream(path + "\\lty.txt");
        String string = "TianYiLuo\r\n0712";
        byte[] bytes = string.getBytes();
        f1.write(bytes);
        f1.flush();
        f1.close();
    }

    public static void fileInputStream() throws IOException {
        FileInputStream f2 = new FileInputStream(path + "\\lty.txt");
//        System.out.println(Arrays.toString(f2.readAllBytes()));
        int b;
        while ((b = f2.read()) != -1) {
            System.out.print(b + " ");
        }
        f2.close();
    }

    public static void fileCopy() throws IOException {
        File f1 = new File(path + "\\芝士雪豹.mp3");
        FileInputStream fis = new FileInputStream(f1);
        File f2 = new File(path + "\\zh.mp3");
        FileOutputStream fos = new FileOutputStream(f2);
        byte[] b = new byte[1024 * 1024 * 5];
        int len;
        long s = System.currentTimeMillis();
        while ((len = fis.read(b)) != -1) {
            fos.write(b, 0, len);
        }
        long e = System.currentTimeMillis();
        System.out.println(e - s);
        fis.close();
        fos.close();
    }

    public static void fileReader() throws IOException {
        File file = new File(path + "\\lty.txt");
        FileReader reader = new FileReader(file);
        int fr;
        char[] c = new char[2];
        while ((fr = reader.read(c)) != -1) {
            System.out.print(new String(c, 0, fr));
        }
        reader.close();
    }

    public static void fileWriter() throws IOException {
        File file = new File(path + "\\lty.txt");
        FileWriter writer = new FileWriter(file, true);
        String s = "\n我是艾斯比";
        writer.write(s);
        writer.flush();
        writer.close();
    }

    /**
     * 字节缓冲流
     *
     * @throws IOException
     */
    public static void bufferedStream() throws IOException {
        File inFile = new File(path + "\\lty.txt");
        File outFile = new File(path + "\\0712.txt");
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(inFile));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFile));
        int b;
        while ((b = bis.read()) != -1) {
            bos.write(b);
        }
        bis.close();
        bos.close();
        System.out.println("over");
    }

    /**
     * 字符缓冲流
     *
     * @throws IOException
     */
    public static void buffered() throws IOException {
        File inFile = new File(path + "\\lty.txt");
        File outFile = new File(path + "\\0712.txt");
        BufferedReader bfr = new BufferedReader(new FileReader(inFile));
        BufferedWriter bfw = new BufferedWriter(new FileWriter(outFile));
        String line;
        while ((line = bfr.readLine()) != null) {
            System.out.println(line);
            bfw.write(line);
            bfw.newLine();
        }
        bfr.close();
        bfw.close();
    }

    /**
     * 转换流
     */
    public static void changer() throws IOException {
        File file = new File(path, "lty.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        reader.close();
    }

    /**
     * 序列化流
     * 将JavaBean对象序列化存入文件
     * Serializable 先实现
     */
    public static void objectStream() throws IOException, ClassNotFoundException {
        File file = new File(path, "\\students.txt");
        if (!file.exists()) {
            file.createNewFile();
        }
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
        Student student = new Student("洛天依", 15, "地球");
        oos.writeObject(student);
        oos.close();

        //反序列化
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        Student s = (Student) ois.readObject();
        System.out.println(s);
        ois.close();
    }

    /**
     * 序列化多个对象 List实现
     */
    public static void listStudent() throws IOException, ClassNotFoundException {
        File file = new File(path, "\\listStudents.txt");
        ArrayList<Student> arrayList = new ArrayList<>();
        arrayList.add(new Student("洛天依", 15, "地球"));
        arrayList.add(new Student("洛依", 16, "金星"));
        arrayList.add(new Student("洛天", 17, "水星"));
        arrayList.add(new Student("洛地依", 18, "火星"));
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
        oos.writeObject(arrayList);
        oos.close();
        //读取
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        ArrayList<Student> list = (ArrayList<Student>) ois.readObject();
        System.out.println(list);
    }

    /**
     * 字节/字符输出流
     */
    public static void printStream() throws IOException {
        File file = new File(path, "我是艾斯比.txt");
        PrintStream ps = new PrintStream(new FileOutputStream(file), true);
        ps.println("我是一个艾斯比");
        ps.print("一个真正的艾斯比");
        ps.printf("%d是我爱刷的%n", 114);
        ps.close();

        PrintWriter printWriter = new PrintWriter(new FileWriter(file, true), true);
        printWriter.println("我也是一个艾斯比");
        printWriter.print("埃斯比不是我");
        printWriter.printf("%.3f 是我要加的", 0.514);
    }

    /**
     * 解压缩流
     */
    public static void zipInputStream() throws IOException {
        File file = new File(path, "\\package.zip");
        ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(file));
        ZipEntry entry;
        while ((entry = zipInputStream.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                File file1 = new File(path, entry.toString());
                file1.mkdirs();
            } else {
                File file2 = new File(path, entry.toString());
                FileOutputStream fos = new FileOutputStream(file2);
                int b;
                while ((b = zipInputStream.read()) != -1) {
                    fos.write(b);
                }
                fos.close();
                zipInputStream.closeEntry();
            }

        }
    }

    public static void zipOutputStream() throws IOException {
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(path, "\\pacckge2.zip")));
//        创建压缩包中的文件 ，此时为空文件
        ZipEntry entry = new ZipEntry("lty1.png");
//        把空文件放入压缩包
        zos.putNextEntry(entry);
        int b;
//        写入文件数据
        FileInputStream fis = new FileInputStream(new File(path, "\\lty1.png"));
        while ((b = fis.read()) != -1) {
            zos.write(b);
        }
        fis.close();
        zos.closeEntry();
        zos.close();
    }
}
